﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Rugicon
{
    /// <summary>
    /// Emoticons service that pulls the data from Live Messenger.
    /// </summary>
    public class LiveEmoticonService : IEmoticonService
    {
        /// <summary>
        /// Raised when the user added or removed emoticons from the Live Messenger folder.
        /// </summary>
        public event EventHandler<EventArgs> EmoticonsChanged;

        /// <summary>
        /// Used for being notified when the user adds or removes its emoticons.
        /// </summary>
        private FileSystemWatcher fileWatcher;

        /// <summary>
        /// Creates an instance of <see cref="LiveMessengerService"/> and initializes it.
        /// </summary>
        public LiveEmoticonService()
        {
            // Set up the emoticons monitoring.
            // Look for dt2 files below the root Live Messenger data folder.
            // This is, all live accounts.
            fileWatcher = new FileSystemWatcher();
            fileWatcher.Path = LiveMessenger.RootFolderPath;
            fileWatcher.Created += (s, e) => { OnEmoticonsChanged(); };
            fileWatcher.Deleted += (s, e) => { OnEmoticonsChanged(); };
            fileWatcher.Renamed += (s, e) => { OnEmoticonsChanged(); };
            fileWatcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.CreationTime;
            fileWatcher.Filter = "*.dt2";
            fileWatcher.IncludeSubdirectories = true;
            fileWatcher.EnableRaisingEvents = true;
        }

        /// <summary>
        /// Called when the user added or removed emoticons from the Live Messenger folder.
        /// </summary>
        protected void OnEmoticonsChanged()
        {
            if (this.EmoticonsChanged != null)
                EmoticonsChanged(this, new EventArgs());
        }

        /// <summary>
        /// Returns the available emoticon sets from Live Messenger.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<EmoticonSet> GetEmoticonSets()
        {
            // This service has only one set of emoticons,
            // the Live Messenger custom emoticons.
            // This set includes all emoticons of all users.
            var set = new EmoticonSet() { Name = "Live Messenger custom emoticons" };

            // Map from LiveEmoticon to Emoticon.
            // There is no real need to let LiveEmoticon class live, by the way.
            foreach (var emoticon in LiveMessenger.GetEmoticons())
            {
                set.Emoticons.Add(new Emoticon
                {
                    Filename = emoticon.Filename,
                    Hash = emoticon.Hash
                });
            }

            // Return the only set available.
            return new List<EmoticonSet> { set };
        }

        public void Dispose()
        {
            fileWatcher.Dispose();
        }
    }
}
